﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Objects;
using CObject;
using Objects.CMT;
using System.Data;
using System.Drawing;
using System.Text;

public partial class ClientRegistration : cPage
{
    DataTable dtCountry;
    CMTClientRegistration clientReg;

    #region Properties

    public string AttendeeId
    {
        get
        {

            if (ddlClient.SelectedValue != "0")
            {
                string[] strClient = ddlClient.SelectedValue.Split("#".ToArray());
                if (strClient.Length > 0)
                {
                    return strClient[0];
                }
                else
                {
                    return ddlClient.SelectedValue;
                }
            }
            else
                return ddlClient.SelectedValue;
        }
    }
    public string OfficeName
    {
        get
        {

            if (ddlClient.SelectedValue != "0")
            {
                string[] strClient = ddlClient.SelectedValue.Split("#".ToArray());
                if (strClient.Length > 1)
                {
                    return strClient[1];
                }
                else
                {
                    return string.Empty;
                }
            }
            else
                return string.Empty;
        }
    }
    #endregion


    #region private methods

    private void GetClientCountries()
    {
        DataTable dtClientCountry = CMTClientRegistration.GetClientCountryDetails(ViewState["EventId"].ToString());
        lstCountry.DataSource = dtClientCountry;
        lstCountry.DataBind();
    }
    private void clearControls()
    {

        txtName.Text = "";
        ddlCountry.SelectedItem.Value = "0";
        txtIndustry.Text = "";
        txtMobile.Text = "";
        txtEmail.Text = "";
        txtDesignation.Text = "";
        txtPreference.Text = "";
        rblPartcipateConfirm.SelectedValue = "1";
        txtxApprTimeReq.Text = "";

        ddlMeetingType.SelectedValue = "0";

        txtCateringReq.Text = "";
        txtSpecificIssues.Text = "";

        rblPrivateMeeting.SelectedValue = "0";
        HideVisiblePrivatetab();
        rblTranslators.SelectedValue = "0";
        rblPriorityClient.SelectedValue = "0";
        ddlPrivateCountry.SelectedValue = "0";

        txtTimeSlot1StartDate.Text = "";
        ddlMeetingStartTimeHrsTimeSlot1.ClearSelection();
        ddlMeetingStartTimeMnsTimeSlot1.ClearSelection();

        txtTimeSlot1EndDate.Text = string.Empty;
        ddlMeetingEndTimeHrsTimeslot1.ClearSelection();
        ddlMeetingEndTimeMnsTimeSlot1.ClearSelection();
        txtTimeSlot2StartDate.Text = "";
        ddlMeetingStartTimeHrsTimeslot2.ClearSelection();
        ddlMeetingStartTimeMnsTimeslot2.ClearSelection();

        txtTimeSlot2EndDate.Text = "";
        ddlMeetingEndTimeHrsTimeSlot2.ClearSelection();
        ddlMeetingEndTimeMnsTimeslot2.ClearSelection();

        txtTimeSlot3StartDate.Text = "";
        ddlMeetingStartTimeHrsTimeslot3.ClearSelection();
        ddlMeetingStartTimeMnsTimeslot3.ClearSelection();
        txtTimeSlot3EndDate.Text = "";
        ddlMeetingEndTimeHrsTimeSlot3.ClearSelection();
        ddlMeetingEndTimeMnsTimeslot3.ClearSelection();

        txtPrivateMeetingDate.Text = "";
        ddlPrivateMeetingHrs.ClearSelection();
        ddlPrivateMeetingMins.ClearSelection();

        chklstMeetingReq.ClearSelection();

        lstCountryRepresent.Items.Clear();


    }

    private void PopulateCountry()
    {
        dtCountry = CMTClientRegistration.GetCountryDetails();
        ddlCountry.DataSource = dtCountry;
        ddlCountry.DataBind();
        ddlPrivateCountry.DataSource = dtCountry;
        ddlPrivateCountry.DataBind();

    }
    private void PopulateMeetingTypes()
    {
        MeetingType meetingTypes = new MeetingType();
        CCollection loColl = new CCollection(new MeetingType(), true);
        DataTable dtMeetingTypeList = loColl.DataTable;
        ddlMeetingType.DataSource = dtMeetingTypeList;
        ddlMeetingType.DataBind();

    }
    private void SetDefaultvaluesInDroplist()
    {
        ListItem defaultItem = new ListItem("Select Country", "0");
        ddlCountry.Items.Insert(0, defaultItem);
        ddlPrivateCountry.Items.Insert(0, defaultItem);
        defaultItem = new ListItem("Select Meeting Type", "0");
        ddlMeetingType.Items.Insert(0, defaultItem);
        defaultItem = new ListItem("Select Client", "0");
        ddlClient.Items.Insert(0, defaultItem);

    }
    private void PopulateClientsAgainstEventAttendeeId()
    {
        DataTable dtEventAttendee = CMTClientRegistration.GetClientsForHost(GetEventAttendeeId());
        if (dtEventAttendee != null && dtEventAttendee.Rows.Count > 0)
        {
            ViewState["EventId"] = dtEventAttendee.Rows[0]["EventId"];
            GetClientCountries();
        }

        foreach (DataRow dtrAttendee in dtEventAttendee.Rows)
        {
            ddlClient.Items.Add(new ListItem(dtrAttendee["Name"].ToString(), dtrAttendee["EventAttendeeID"].ToString() + "#" + dtrAttendee["Office"].ToString()));
        }


    }
    private void PopulateClientRegistrationDetailForAttendee(int attendeeId)
    {
        DataTable dtClientReg = CMTClientRegistration.GetRegistrationDetailsForAnAttendee(AttendeeId);

        if (dtClientReg != null && dtClientReg.Rows.Count > 0)
        {
            foreach (DataRow row in dtClientReg.Rows)
            {
                SetClientRegistration(row);
            }
        }

    }

    private void SetClientRegistration(DataRow clientRegRow)
    {

        ViewState["ClientId"] = clientRegRow["ClientId"].ToString();

        txtName.Text = clientRegRow["Name"].ToString();
        if (!string.IsNullOrEmpty(clientRegRow["Country"].ToString()))
        {
            ddlCountry.SelectedItem.Text = clientRegRow["Country"].ToString();
        }
        txtIndustry.Text = clientRegRow["Industry"].ToString();
        txtMobile.Text = clientRegRow["Mobile"].ToString();
        txtEmail.Text = clientRegRow["EmailAddress"].ToString();
        txtDesignation.Text = clientRegRow["Role"].ToString();
        txtPreference.Text = clientRegRow["Preferences"].ToString();
        rblPartcipateConfirm.SelectedValue = ConvertBooleanTostring(Convert.ToBoolean(clientRegRow["IsParticipationConfirmed"]));
        txtxApprTimeReq.Text = clientRegRow["ApproximateTimeReq"].ToString();
        if (!string.IsNullOrEmpty(clientRegRow["MeetingTypes"].ToString()))
        {
            ddlMeetingType.SelectedValue = clientRegRow["MeetingTypes"].ToString();
        }


        txtCateringReq.Text = clientRegRow["CateringRequirements"].ToString();
        txtSpecificIssues.Text = clientRegRow["SpecialIssues"].ToString();

        rblPrivateMeeting.SelectedValue = ConvertBooleanTostring(Convert.ToBoolean(clientRegRow["IsPrivateMeetings"].ToString()));
        rblTranslators.SelectedValue = ConvertBooleanTostring(Convert.ToBoolean(clientRegRow["RequirementofTranslators"].ToString()));
        rblPriorityClient.SelectedValue = ConvertBooleanTostring(Convert.ToBoolean(clientRegRow["IsPriorityClient"].ToString()));


        if (!string.IsNullOrEmpty(clientRegRow["PrivateMeetingLocation"].ToString()))
        {
            ddlPrivateCountry.SelectedItem.Text = clientRegRow["PrivateMeetingLocation"].ToString();
        }

        txtTimeSlot1StartDate.Text = SetDate(ddlMeetingStartTimeHrsTimeSlot1, ddlMeetingStartTimeMnsTimeSlot1, Convert.ToDateTime(clientRegRow["AvailableStartDate1"]));
        txtTimeSlot1EndDate.Text = SetDate(ddlMeetingEndTimeHrsTimeslot1, ddlMeetingEndTimeMnsTimeSlot1, Convert.ToDateTime(clientRegRow["AvailableEndDate1"]));
        txtTimeSlot2StartDate.Text = SetDate(ddlMeetingStartTimeHrsTimeslot2, ddlMeetingStartTimeMnsTimeslot2, Convert.ToDateTime(clientRegRow["AvailableStartDate2"]));
        txtTimeSlot2EndDate.Text = SetDate(ddlMeetingEndTimeHrsTimeSlot2, ddlMeetingEndTimeMnsTimeslot2, Convert.ToDateTime(clientRegRow["AvailableEndDate2"]));
        txtTimeSlot3StartDate.Text = SetDate(ddlMeetingStartTimeHrsTimeslot3, ddlMeetingStartTimeMnsTimeslot3, Convert.ToDateTime(clientRegRow["AvailableStartDate3"]));
        txtTimeSlot3EndDate.Text = SetDate(ddlMeetingEndTimeHrsTimeSlot3, ddlMeetingEndTimeMnsTimeslot3, Convert.ToDateTime(clientRegRow["AvailableEndDate3"]));
        txtPrivateMeetingDate.Text = SetDate(ddlPrivateMeetingHrs, ddlPrivateMeetingMins, Convert.ToDateTime(clientRegRow["PrivateMeetingTime"]));

        setSetupReq(clientRegRow["SetupRequirements"].ToString());
        SetCountries(clientRegRow["PartnerCOuntryName"].ToString());

        HideVisiblePrivatetab();

    }
    private void setSetupReq(string SetupRequirements)
    {
        string[] strReq = SetupRequirements.Split(",".ToArray());
        if (strReq.Length > 0)
        {
            foreach (string Req in strReq)
            {
                for (int i = 0; i < chklstMeetingReq.Items.Count; i++)
                {
                    if (chklstMeetingReq.Items[i].Text == Req)
                    {
                        chklstMeetingReq.Items[i].Selected = true;
                        break;
                    }

                }
            }

        }
    }

    private void SetCountries(string PartnerCOuntryName)
    {
        try
        {
            string[] strCountries = PartnerCOuntryName.Split(",".ToArray());
            foreach (string strCountry in strCountries)
            {

                foreach (ListItem country in lstCountry.Items)
                {
                    if (country.Text == strCountry)
                    {
                        lstCountryRepresent.Items.Add(country);
                        break;
                    }
                }
            }



            foreach (ListItem country in lstCountryRepresent.Items)
            {
                lstCountry.Items.Remove(country);
            }

            if (lstCountryRepresent.Items.Count > 0)
            {
                lstCountryRepresent.SelectedIndex = 0;
            }

        }
        catch (Exception)
        {
            throw;
        }
    }

    private string SetDate(DropDownList ddlHrs, DropDownList ddlMins, DateTime date)
    {
        ListItem startHr = ddlHrs.Items.FindByValue(date.ToString("HH"));
        if (startHr != null)
        {
            ddlHrs.ClearSelection();
            startHr.Selected = true;
        }

        ListItem startMin = ddlMins.Items.FindByValue(date.ToString("mm"));
        if (startMin != null)
        {
            ddlMins.ClearSelection();
            startMin.Selected = true;
        }
        if (date.Year > 1753)
            return date.ToShortDateString();
        else
            return "";
    }

    private int GetEventAttendeeId()
    {
        int eventAttendeeID = 0;
        if (!string.IsNullOrEmpty(Request["EventAttendeeID"]))
        {
            eventAttendeeID = Convert.ToInt32(Request["EventAttendeeID"]);
        }
        return eventAttendeeID;
    }
    private bool Registration()
    {
        bool mSuccess = false;
        clientReg = new CMTClientRegistration();
        clientReg.Name = txtName.Text;
        if (ddlCountry.SelectedValue != "0")
            clientReg.Country = ddlCountry.SelectedItem.Text;
        clientReg.Industry = txtIndustry.Text;
        clientReg.Mobile = txtMobile.Text;
        clientReg.EmailAddress = txtEmail.Text;
        clientReg.Role = txtDesignation.Text;
        clientReg.Preferences = txtPreference.Text;
        clientReg.IsParticipationConfirmed = ConvertStringToBoolean(rblPartcipateConfirm.SelectedValue);
        clientReg.RequirementofTranslators = ConvertStringToBoolean(rblTranslators.SelectedValue);
        clientReg.IsPriorityClient = ConvertStringToBoolean(rblPriorityClient.SelectedValue);

        clientReg.ApproximateTimeReq = txtxApprTimeReq.Text;
        clientReg.MeetingTypes = ddlMeetingType.SelectedValue;

        clientReg.AvailableStartDate1 = SetAvailableTimeslots(ddlMeetingStartTimeHrsTimeSlot1.SelectedItem.Text, ddlMeetingStartTimeMnsTimeSlot1.SelectedItem.Text, txtTimeSlot1StartDate.Text);
        clientReg.AvailableEndDate1 = SetAvailableTimeslots(ddlMeetingEndTimeHrsTimeslot1.SelectedItem.Text, ddlMeetingEndTimeMnsTimeSlot1.SelectedItem.Text, txtTimeSlot1EndDate.Text);

        if (DateTime.Compare(clientReg.AvailableStartDate1, clientReg.AvailableEndDate1) > 0)
        {
            custStartEndDate1.IsValid = false;
            return false;
        }


        clientReg.AvailableStartDate2 = SetAvailableTimeslots(ddlMeetingStartTimeHrsTimeslot2.SelectedItem.Text, ddlMeetingStartTimeMnsTimeslot2.SelectedItem.Text, txtTimeSlot2StartDate.Text);
        clientReg.AvailableEndDate2 = SetAvailableTimeslots(ddlMeetingEndTimeHrsTimeSlot2.SelectedItem.Text, ddlMeetingEndTimeMnsTimeslot2.SelectedItem.Text, txtTimeSlot2EndDate.Text);


        if (DateTime.Compare(clientReg.AvailableStartDate2, clientReg.AvailableEndDate2) > 0)
        {
            custStartEndDate2.IsValid = false;
            return false;
        }

        clientReg.AvailableStartDate3 = SetAvailableTimeslots(ddlMeetingStartTimeHrsTimeslot3.SelectedItem.Text, ddlMeetingStartTimeMnsTimeslot3.SelectedItem.Text, txtTimeSlot3StartDate.Text);
        clientReg.AvailableEndDate3 = SetAvailableTimeslots(ddlMeetingEndTimeHrsTimeSlot3.SelectedItem.Text, ddlMeetingEndTimeMnsTimeslot3.SelectedItem.Text, txtTimeSlot3EndDate.Text);


        if (DateTime.Compare(clientReg.AvailableStartDate3, clientReg.AvailableEndDate3) > 0)
        {
            custStartEndDate3.IsValid = false;
            return false;
        }
        clientReg.IsPrivateMeetings = ConvertStringToBoolean(rblPrivateMeeting.SelectedValue);
        if (ddlPrivateCountry.SelectedValue != "0")
            clientReg.PrivateMeetingLocation = ddlPrivateCountry.SelectedItem.Text;
        clientReg.PrivateMeetingTime = SetAvailableTimeslots(ddlPrivateMeetingHrs.SelectedItem.Text, ddlPrivateMeetingMins.SelectedItem.Text, txtPrivateMeetingDate.Text);

        clientReg.SetupRequirements = GetComaSepratedValue(2);
        clientReg.PartnerCOuntryName = GetComaSepratedValue(1);
        clientReg.CateringRequirements = txtCateringReq.Text;
        clientReg.SpecialIssues = txtSpecificIssues.Text;

        clientReg.CreatedBy = CurrentUser.GUI;
        if (ViewState["EventId"] != null)
        {
            clientReg.EventId = Convert.ToInt32(ViewState["EventId"]);
        }
        clientReg.EventAttendeeID = Convert.ToInt32(AttendeeId);
        if (ViewState["ClientId"] != null)
        {
            clientReg.ClientId = Convert.ToInt32(ViewState["ClientId"]);
        }
        try
        {
            clientReg.Save();
            ViewState["ClientId"] = clientReg.ClientId;
            mSuccess = true;
        }
        catch
        {
            mSuccess = false;

        }
        finally
        {
            clientReg = null;
        }

        return mSuccess;
    }
    private bool ConvertStringToBoolean(string value)
    {
        bool mValue = false;
        if (value == "1")
        {
            mValue = true;
        }
        else
        {
            mValue = false;
        }

        return mValue;
    }

    private string ConvertBooleanTostring(bool value)
    {
        string mValue = "1";
        if (value)
        {
            mValue = "1";
        }
        else
        {
            mValue = "0";
        }

        return mValue;
    }
    private string GetComaSepratedValue(int flag)
    {
        StringBuilder comasepratedValue = new StringBuilder();

        if (flag == 1)
        {
            for (int i = 0; i < lstCountryRepresent.Items.Count; i++)
            {
                comasepratedValue.Append(lstCountryRepresent.Items[i].Text);
                if (i < lstCountryRepresent.Items.Count - 1)
                {
                    comasepratedValue.Append(",");
                }
            }
        }
        else
        {
            for (int i = 0; i < chklstMeetingReq.Items.Count; i++)
            {
                if (chklstMeetingReq.Items[i].Selected)
                {
                    comasepratedValue.Append(chklstMeetingReq.Items[i].Text);
                    comasepratedValue.Append(",");

                }

            }
            if (comasepratedValue.Length > 0)
            {
                comasepratedValue.Remove(comasepratedValue.Length - 1, 1);
            }
        }
        return comasepratedValue.ToString();
    }
    private DateTime SetAvailableTimeslots(string timeHrs, string timeMins, string date)
    {
        TimeSpan timeSpan;
        DateTime dateTime = Convert.ToDateTime("1/1/1753");
        timeSpan = new TimeSpan(int.Parse(timeHrs), int.Parse(timeMins), 0);

        if ((string.IsNullOrEmpty(date) == false))
        {
            dateTime = Convert.ToDateTime(date).Add(timeSpan);
        }
        return dateTime;
    }

    private void HideVisiblePrivatetab()
    {
        if (rblPrivateMeeting.SelectedValue == "1")
        {
            tabPrivateMeeting.Visible = true;
        }
        else
        {
            tabPrivateMeeting.Visible = false;
        }
    }
    #endregion

    #region page events

    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        if (ViewState["EventId"] != null)
        {
            DataTable dtClientReg = CMTClientRegistration.GetRegistrationDetailsForAnEvent(Convert.ToInt32(ViewState["EventId"]));

            if (dtClientReg != null)
            {

                CommonRepository.ExportToExcel(dtClientReg, "Client_Registration_Details");
            }
        }
    }
    protected void btnClearAll_Click(object sender, EventArgs e)
    {
        clearControls();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            PopulateCountry();
            PopulateMeetingTypes();
            PopulateClientsAgainstEventAttendeeId();
            SetDefaultvaluesInDroplist();

        }
    }
    protected void rblPrivateMeeting_SelectedIndexChanged(object sender, EventArgs e)
    {
        HideVisiblePrivatetab();
    }
    protected void ddlClient_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (AttendeeId != "0")
        {
            clearControls();
            ViewState["ClientId"] = null;
            txtName.Text = ddlClient.SelectedItem.Text;
            txtOrganisation.Text = OfficeName;
            PopulateClientRegistrationDetailForAttendee(Convert.ToInt32(AttendeeId));
        }


    }
    protected void btnLeft_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (ListItem country in lstCountryRepresent.Items)
            {
                if (country.Selected)
                {
                    lstCountry.Items.Add(country);
                }
            }

            foreach (ListItem country in lstCountry.Items)
            {
                lstCountryRepresent.Items.Remove(country);
            }

            if (lstCountryRepresent.Items.Count > 0)
            {
                lstCountryRepresent.SelectedIndex = 0;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    protected void btnAllLeft_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (ListItem country in lstCountryRepresent.Items)
            {
                lstCountry.Items.Add(country);
            }

            lstCountryRepresent.Items.Clear();
        }
        catch (Exception)
        {
            throw;
        }
    }
    protected void btnAllRight_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (ListItem country in lstCountry.Items)
            {
                lstCountryRepresent.Items.Add(country);
            }

            lstCountry.Items.Clear();

            if (lstCountryRepresent.Items.Count > 0)
            {
                lstCountryRepresent.SelectedIndex = 0;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    protected void btnRight_Click(object sender, EventArgs e)
    {
        try
        {
            foreach (ListItem country in lstCountry.Items)
            {
                if (country.Selected)
                {
                    lstCountryRepresent.Items.Add(country);
                }
            }

            foreach (ListItem country in lstCountryRepresent.Items)
            {
                lstCountry.Items.Remove(country);
            }

            if (lstCountryRepresent.Items.Count > 0)
            {
                lstCountryRepresent.SelectedIndex = 0;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
        if (AttendeeId != "0")
        {

            string message = string.Empty;
            if (Registration())
            {
                lblRegistrationMessage.Text = ParticipantRegistrationClass.RegistrationCompleated;
                lblRegistrationMessage.ForeColor = Color.Green;

            }
            else
            {
                lblRegistrationMessage.Text = "Some Technical issue in Client Registration!";
                lblRegistrationMessage.ForeColor = Color.Red;

            }
            lblRegistrationMessage.Visible = true;
        }
    }

    #endregion



}